package com.fanshuai.jmx.tomcat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

@ComponentScan(basePackages = "com.fanshuai.jmx.tomcat")
@EnableAutoConfiguration(
        exclude = DataSourceAutoConfiguration.class
)
public class TomcatMain {
    public static void main(String[] args) {
        SpringApplication.run(TomcatMain.class, args);
        jmx();
    }

    public static void jmx() {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        try {
            GCMBean(mBeanServer);
            TomcatMBean(mBeanServer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void GCMBean(MBeanServer mBeanServer) throws Exception {
        /**
         * 通过ManagementFactory获取JVM注册的MBean:
         * thread, memory, memory pool, garbage collector, class loading, compilation等
         */
        List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean mxBean : garbageCollectorMXBeans) {
            if (mxBean.getName().equals("G1 Young Generation")) {
                System.out.println(mxBean.getCollectionTime() + "," + mxBean.getCollectionCount());
            }
        }

        /**
         * 初始化MBeanServer, 通过ObjectName获取MBean
         */
        ObjectName objectName = new ObjectName("java.lang:type=GarbageCollector,name=G1 Young Generation");

        long CollectionTime = (long) mBeanServer.getAttribute(objectName, "CollectionTime");
        long CollectionCount = (long) mBeanServer.getAttribute(objectName, "CollectionCount");
        System.out.println(CollectionTime + "," + CollectionCount);
    }

    private static void TomcatMBean(MBeanServer mBeanServer) throws Exception {
        ObjectName objectName = new ObjectName("Tomcat:type=ThreadPool,name=\"http-nio-8080\"");

        /**
         * acceptor
         */
        int acceptorThreadCount = (int) mBeanServer.getAttribute(objectName, "acceptorThreadCount");
        int acceptorThreadPriority = (int) mBeanServer.getAttribute(objectName, "acceptorThreadPriority");
        int acceptCount = (int) mBeanServer.getAttribute(objectName, "acceptCount");
        System.out.println(acceptCount);

        System.out.println(acceptorThreadCount + ", " + acceptorThreadPriority);

        /**
         * connection
         */
        long connectionCount = (long) mBeanServer.getAttribute(objectName, "connectionCount");
        int connectionTimeout = (int) mBeanServer.getAttribute(objectName, "connectionTimeout");
        int maxConnections = (int) mBeanServer.getAttribute(objectName, "maxConnections");

        System.out.println(connectionCount);
        System.out.println(connectionTimeout);
        System.out.println(maxConnections);

        /**
         * threads
         */
        int currentThreadCount = (int) mBeanServer.getAttribute(objectName, "currentThreadCount");
        int currentThreadsBusy = (int) mBeanServer.getAttribute(objectName, "currentThreadsBusy");
        int maxThreads = (int) mBeanServer.getAttribute(objectName, "maxThreads");

        System.out.println(currentThreadCount);
        System.out.println(currentThreadsBusy);
        System.out.println(maxThreads);
    }
}
